package cn.gemiman.daoyi.module.cms.controller.admin.appsitealbum;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gemiman.daoyi.module.cms.controller.admin.appsitealbum.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitealbum.AppSiteAlbumDO;
import cn.gemiman.daoyi.module.cms.service.appsitealbum.AppSiteAlbumService;

@Tag(name = "管理后台 - 站点相册")
@RestController
@RequestMapping("/cms/app-site-album")
@Validated
public class AppSiteAlbumController {

    @Resource
    private AppSiteAlbumService appSiteAlbumService;

    @PostMapping("/create")
    @Operation(summary = "创建站点相册")
    @PreAuthorize("@ss.hasPermission('cms:app-site-album:create')")
    public CommonResult<Long> createAppSiteAlbum(@Valid @RequestBody AppSiteAlbumSaveReqVO createReqVO) {
        return success(appSiteAlbumService.createAppSiteAlbum(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新站点相册")
    @PreAuthorize("@ss.hasPermission('cms:app-site-album:update')")
    public CommonResult<Boolean> updateAppSiteAlbum(@Valid @RequestBody AppSiteAlbumSaveReqVO updateReqVO) {
        appSiteAlbumService.updateAppSiteAlbum(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除站点相册")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:app-site-album:delete')")
    public CommonResult<Boolean> deleteAppSiteAlbum(@RequestParam("id") Long id) {
        appSiteAlbumService.deleteAppSiteAlbum(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得站点相册")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:app-site-album:query')")
    public CommonResult<AppSiteAlbumRespVO> getAppSiteAlbum(@RequestParam("id") Long id) {
        AppSiteAlbumDO appSiteAlbum = appSiteAlbumService.getAppSiteAlbum(id);
        return success(BeanUtils.toBean(appSiteAlbum, AppSiteAlbumRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得站点相册分页")
    @PreAuthorize("@ss.hasPermission('cms:app-site-album:query')")
    public CommonResult<PageResult<AppSiteAlbumRespVO>> getAppSiteAlbumPage(@Valid AppSiteAlbumPageReqVO pageReqVO) {
        PageResult<AppSiteAlbumDO> pageResult = appSiteAlbumService.getAppSiteAlbumPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppSiteAlbumRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出站点相册 Excel")
    @PreAuthorize("@ss.hasPermission('cms:app-site-album:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppSiteAlbumExcel(@Valid AppSiteAlbumPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppSiteAlbumDO> list = appSiteAlbumService.getAppSiteAlbumPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "站点相册.xls", "数据", AppSiteAlbumRespVO.class,
                        BeanUtils.toBean(list, AppSiteAlbumRespVO.class));
    }

}